<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Wyp
 */

namespace App\Modules\Live\Controllers;

use App\Modules\Live\Services\LiveService;
use App\Modules\RedBag\Services\RedBagService;
use App\Modules\Coupon\Services\CouponService;
use App\Modules\Live\Services\PushUrl;
use App\Modules\Live\Services\TLSSigAPIv2;
use App\Modules\Live\Validators\LiveControllerValidator;
use Flower\Core\Http\BaseController;
use Illuminate\Http\Request;
use App\Lib\Facades\FLAuth;

class LiveController extends BaseController
{

    private $liveService;
    protected $redBagService;
    protected $couponService;

    public function __construct(LiveService $liveService,RedBagService $redBagService,CouponService $couponService)
    {
        $this->liveService = $liveService;
        $this->redBagService = $redBagService;
        $this->couponService = $couponService;
    }

    public function getRules()
    {
        return LiveControllerValidator::validator();
    }

    /**
     * 获取直播信息
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getLiveInfo(Request $request) {
        $live_id = $request->input('live_id');
        $data = $this->liveService->getLiveInfo($live_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 获取直播数据
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getLiveData(Request $request) {
        $live_id = $request->input('live_id');
        $data = $this->liveService->getLiveData($live_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 获取用户昵称
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getMemberNick(Request $request) {
        $member_id = $request->input('member_id');
        $data = $this->liveService->getMemberInfo($member_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 创建直播间
     * @param Request $request
     * @param PushUrl $pushUrl
     * @return mixed
     */
    public function createLive(Request $request,PushUrl $pushUrl)
    {
        $name = $request->input('name');
        $user_id = FLAuth::id();
        $picture = $request->input('picture');
        $store_id = $request->input('store_id');
        $ex_time = date("Y-m-d H:i:s",time()+60*60*24);
//        $cname = env('CNAME_PUSH');
        $cname = '67680.livepush.myqcloud.com';//先用默认测试
        $live_key = env('LIVE_KEY');
        $stream_name = FLAuth::id();
        $url = $pushUrl->getPushUrl($cname,$stream_name,$live_key,$ex_time);
        $rtmp = "rtmp://play.tiechuikeji.com/live/".$user_id;
        $flv = "http://play.tiechuikeji.com/live/".$user_id.'.flv';//拉流地址

        $live = $this->liveService->createLive($name,$user_id,$picture,$url,$store_id,$rtmp,$flv);

        $data = [
            'live_id' => $live->id
        ];

        return $this->display($data, '创建成功');
    }

    /**
     * 退出直播间
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     * @throws \App\Exceptions\NotFoundException
     */
    public function editLive(Request $request)
    {
        $live_id = $request->input('live_id');
        $this->liveService->editLive($live_id);
        $store = $this->liveService->getStoreId($live_id);
        //回收红包
        $userId = FLAuth::id();
        $this->redBagService->backRedBag($userId,$store['store_id']);
        //回收优惠券
        $this->couponService->backNoReceiveCoupon($userId,$store['store_id']);

        return $this->success('修改成功');
    }

    /**
     * 获取在线用户列表
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getLiveMemberListPage(Request $request)
    {

        $live_id = $request->input('live_id');
        $pageSize = $request->input('pageSize',10);
        $page = $request->input('page',1);
        $member_list = $this->liveService->getLiveMemberListPage($live_id,$pageSize,$page);

        return $this->display($member_list, '返回成功');
    }
    /**
     * 获取直播信息
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getRoomInfo(Request $request) {
        $live_id = $request->input('live_id');
        $data = $this->liveService->getRoomInfo($live_id);
        return $this->display($data, '查询成功');
    }

    /**
     * 关注
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createLiveAttention(Request $request)
    {
        $user_id = FLAuth::id();
        $member_id = $request->input('member_id');
        $live_attention = $this->liveService->addLiveAttention($member_id,$user_id);

        $data = [
            'id' => $live_attention->id
        ];

        return $this->display($data, '关注成功');
    }

    /**
     * 上热门
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createHot(Request $request)
    {
        $live_id = $request->input('live_id');
        $live = $this->liveService->addHot($live_id);

        $data = [
            'id' => $live->id
        ];

        return $this->display($data, '操作成功');
    }

    /**
     * 取消关注
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function updateLiveAttention(Request $request)
    {
        $user_id = FLAuth::id();
        $member_id = $request->input('member_id');
        $live_attention = $this->liveService->updateLiveAttention($member_id,$user_id);

        $data = [
            'id' => $live_attention->id
        ];

        return $this->display($data, '操作成功');
    }

    /**
     * 主播认证
     * @param Request $request
     * @return mixed
     */
    public function createAuthentication(Request $request)
    {
        $identity = $request->input('identity');
        $user_id = FLAuth::id();
        $name = $request->input('name');
        $authentication = $this->liveService->addAuthentication($identity,$user_id,$name);

        $data = [
            'id' => $authentication->id
        ];

        return $this->display($data, '认证成功');
    }

    /**
     * 获取认证
     * @param Request $request
     * @return mixed
     */
    public function getAuthentication()
    {
        $user_id = FLAuth::id();
        $authentication = $this->liveService->getAuthentication($user_id);

        if($authentication) {
            $authentication = 1;
        }else {
            $authentication = 0;
        }
        $data = [
            'authentication' => $authentication
        ];

        return $this->display($data, '获取成功');
    }

    /**
     * 获取腾讯签名
     * @return mixed
     * @throws \Exception
     */
    public function getSig() {
        $user_id = FLAuth::id();
        $api = new TLSSigAPIv2(env('SDK_APP_ID'),env('PRIVATE_KEY_STRING'));
        $sig = $api->genSig('user'.$user_id);//生成usersig
        $data = [
            'sig' => $sig,
            'sdkappid' => env('SDK_APP_ID')
        ];
        return $this->display($data, '获取成功');
    }
}
